package cn.sinata.wanglin.mine.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Industryinfo;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class AlterindustryActivity extends DialogActivity {
    @BindView(R.id.tv_currentindustry)
    TextView tv_currentindustry;//当前行业
    @BindView(R.id.rv_choiceindustry)
    RecyclerView rv_choiceindustry;//
    BaseRecyclerAdapter choiceIndustryAdapter;
    private ArrayList<Industryinfo> mdatas=new ArrayList<Industryinfo>();
    private String industry;
    @Override
    protected int setContentLayout() {
        return R.layout.activity_alterindustry;
    }

    @Override
    protected void initView() {
        Bundle mBundle=getIntent().getExtras();
        if(mBundle!=null){
            industry = mBundle.getString("industry");
            tv_currentindustry.setText(industry);
        }
        mdatas.add(new Industryinfo("互联网",false));
        mdatas.add(new Industryinfo("游戏",false));
        mdatas.add(new Industryinfo("金融",false));
        mdatas.add(new Industryinfo("银行",false));
        mdatas.add(new Industryinfo("房地产",false));
        mdatas.add(new Industryinfo("工程",false));
        mdatas.add(new Industryinfo("设计师",false));
        mdatas.add(new Industryinfo("艺术",false));
        mdatas.add(new Industryinfo("广告传媒",false));
        mdatas.add(new Industryinfo("咨询业",false));
        mdatas.add(new Industryinfo("商贸",false));
        mdatas.add(new Industryinfo("教育培训",false));
        mdatas.add(new Industryinfo("医药",false));
        mdatas.add(new Industryinfo("政府",false));
        mdatas.add(new Industryinfo("交通",false));
        mdatas.add(new Industryinfo("旅游",false));
        mdatas.add(new Industryinfo("酒店餐饮",false));
        mdatas.add(new Industryinfo("学校",false));
        mdatas.add(new Industryinfo("能源",false));
        mdatas.add(new Industryinfo("制造",false));
        mdatas.add(new Industryinfo("其它",false));
        if(choiceIndustryAdapter==null){
            GridLayoutManager mgr=new GridLayoutManager(this,3);
            rv_choiceindustry.setLayoutManager(mgr);
            choiceIndustryAdapter=new BaseRecyclerAdapter<Industryinfo>(mdatas,R.layout.item_choiceindustry) {
                @Override
                public void onBind(final int position,final Industryinfo data, ViewHolder holder) {
                     holder.setText(R.id.cb_choiceindustry,data.getName());
                    ((CheckBox)holder.bind(R.id.cb_choiceindustry)).setChecked(data.ischecked());
                     holder.bind(R.id.cb_choiceindustry).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                               for(int i=0;i<mdatas.size();i++){
                                   mdatas.get(i).setIschecked(false);
                               }
                            tv_currentindustry.setText( mdatas.get(position).getName());
                            CheckBox mCheckBox= (CheckBox)view;
                            mCheckBox.setChecked(true);
                            mdatas.get(position).setIschecked(true);
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            rv_choiceindustry.setAdapter(choiceIndustryAdapter);
        }
    }

    @OnClick({R.id.tv_sureindustry,R.id.iv_accoutlogin_delete})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sureindustry:
                //描述:确定选择行业

                for(int i=0;i<mdatas.size();i++){
                    if(mdatas.get(i).ischecked()){
                        industry=mdatas.get(i).getName();
                    }
                }
                if(TextUtils.isEmpty(industry)){
                    showToast("请先选择行业！");
                    return;
                }
                alterUserinfo(industry,Constant.ALT_industry);
                break;
            case R.id.iv_accoutlogin_delete:
                //描述:结束
                finish();
                break;
        }
    }
    /**
     * 修改用户相关信息，除了标签以外
     * @param industry
     * @param tag
     */
    private  void alterUserinfo( final String industry,int tag){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        Observable<ResultData<JsonObject>> observable = HttpManager.updUser(""+userid,industry,tag);
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        Intent intent=new Intent();
                        intent.putExtra("industry",industry);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });

    }
}